from enum import Enum
from fastapi import FastAPI

app = FastAPI()

'''
1.1 声明路径参数
'''
@app.get("/items1/{item_id}")
async def read_item(item_id):
    print(type(item_id))
    return {"item_id": item_id}

'''
1.2 声明限制类型的路径参数
'''
@app.get("/items2/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}



'''
1.4 预设值
'''
#枚举类
class ModelName(str, Enum):
    yinyu = "yinyu_v"
    s1 = "s1_v"
    s2 = "s2_v"

'''包含枚举的路径参数'''
@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    # 第 1 种判断方式
    if ModelName is ModelName.yinyu:
        return {"model_name": model_name, "message": "yinyu get"}
    # 第 2 种判断方式，效果一样
    if model_name.value == "s1_name":
        return {"model_name": model_name, "message": "s1 get"}
    else:
        return {"model_name": ModelName.s2, "message": "s2 get"}


'''
1.5 包含路径的路径参数
'''
#参数的名称为 file_path，结尾部分的 :path 说明该参数应匹配任意的路径
@app.get("/files/{file_path:path}")
async def read_file(file_path: str):
    return {"file_path": file_path}






if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)